﻿//national.js
var adUtils=
{
    entityAd:
    {
    }
    ,showEntityAd:function(entityId)
    {
        if(this.entityAd[entityId])
        {
            this.renderEntityAd(this.entityAd[entityId]);
        }
        else
        {
            this.parseEntityAd(entityId);
        }
    }
    ,parseEntityAd:function(entityId)
    {
        var ids=[50,51,52];
        var url=mapApi.getUrl('entityAd',
        {
            id:ids.join(','),entityId:entityId
        }
        );
        mapApi.loadXML2(url,fun.bind(this));
        function fun(value)
        {
            if(value)
            {
                try
                {
                    var o=eval('('+value+')');
                    this.entityAd[entityId]=o||
                    {
                    };
                }
                catch(e)
                {
                    this.entityAd[entityId]=
                    {
                    };
                }
            }
            else
            {
                this.entityAd[entityId]=
                {
                };
                return;
            }
            this.renderEntityAd(this.entityAd[entityId]);
        }
    }
    ,getAd:function(ad)
    {
        if(!ad.ex)
        {
            ad.ex=
            {
            };
            var total=0;
            for(var i=0;i<ad.length;i++)
            {
                total+=ad[i].score;
            }
            ad.ex.total=total;
        }
        var rnd=Math.floor(ad.ex.total*Math.random())+1;
        var num=0,pos=0;
        for(var i=0;i<ad.length;i++)
        {
            num+=ad[i].score;
            if(rnd<=num)
            {
                pos=i;
                break;
            }
        }
        return ad[pos];
    }
    ,renderEntityAd:function(entityAd)
    {
        var ids=[50,51,52];
        for(var i=0;i<ids.length;i++)
        {
            var item=ids[i];
            var ads=entityAd[item];
            if(!ads||ads.length==0)
            {
                $('map_comOdDiv'+i).innerHTML='';
                continue;
            }
            var ad=this.getAd(ads);
            var htmlStr='';
            if(this.getFileExt(ad.src)=='swf') 
                htmlStr=this.getFlashHtml(ad.src,105,50);
            else 
                htmlStr='<a href="'+ad.url+'" target="_blank"><img src="'+ad.src+'" style="cursor:pointer;width:105px;height:50px;"></a>';
            $('map_comOdDiv'+i).innerHTML=htmlStr;
        }
    }
    ,getFlashHtml:function(src,w,h)
    {
        var htmlStr='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="{$Width}" height="{$Height}"> <param name="movie" value="{$Src}"> <param name="quality" value="high"> <param name="wmode" value="transparent"> <embed src="{$Src}" width="{$Width}" height="{$Height}" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" wmode="transparent"></embed></object>';
        htmlStr=htmlStr.replace('{$Src}',src).replace('{$Width}',w).replace('{$Height}',h);
        return htmlStr;
    }
    ,getFileExt:function(file)
    {
        if(!file) return '';
        var arr=file.split('.');
        return arr[arr.length-1].toLowerCase();
    }
    ,countEntityAd:function(id)
    {
        var url=mapApi.getUrl('entityAdCount',{id:id});
        mapApi.loadXML2(url,function(){});
    }
};